//
//  UIScrollView+MJRefresh.swift
//  TestSwift
//
//  Created by Song on 2018/10/31.
//  Copyright © 2018 Song. All rights reserved.
//

import UIKit
import MJRefresh

extension UIScrollView {
    
    /// 添加头部刷新
    ///
    /// - Parameter refreshingBlock: 刷新回调
    func qs_addHeaderRefresh(_ refreshingBlock: @escaping MJRefreshComponentRefreshingBlock) {
        mj_header = MJRefreshNormalHeader(refreshingBlock: refreshingBlock)
    }
    
    /// 添加尾部加载更多
    ///
    /// - Parameter refreshingBlock: 刷新回调
    func qs_addFooterRefresh(_ refreshingBlock: @escaping MJRefreshComponentRefreshingBlock) {
        mj_footer = MJRefreshBackNormalFooter(refreshingBlock: refreshingBlock)
    }
    
    /// 开始头部刷新
    func qs_beginHeaderRefresh() {
        mj_header.beginRefreshing()
    }
    
    /// 开始尾部刷新
    func qs_beginFooterRefresh() {
        mj_footer.beginRefreshing()
    }
    
    /// 结束头部刷新
    func qs_endHeaderRefresh() {
        mj_header.endRefreshing()
    }
    
    /// 结束尾部刷新
    ///
    /// - Parameter hasMoreData: 是否有更多数据，true：有更多数据，false：没有更多数据
    func qs_endFooterRefresh(hasMoreData: Bool) {
        if hasMoreData {
            mj_footer.endRefreshing()
        } else {
            mj_footer.endRefreshingWithNoMoreData()
        }
    }
}
